<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2005 by CPG-Nuke Dev Team
  http://www.dragonflycms.com

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/
if (!defined('CPG_NUKE')) { exit; }
Dragonfly_Page::setTitle('. '. _TopLANG);

if (Dragonfly::getKernel()->L10N->multilingual) {
	$querylang = "WHERE language='$currentlang' OR alanguage=''";
	$queryalang = "AND (alanguage='$currentlang' OR alanguage='')"; // top stories
	$queryslang = "WHERE slanguage='$currentlang' "; // top section articles
	$queryplang = "WHERE planguage='$currentlang' "; // top polls
	$queryrlang = "AND language='$currentlang' OR language=''"; // top reviews
} else {
	$querylang = $queryalang = $queryslang = $queryplang = $queryrlang = '';
}

function showTop(\Poodle\SQL\Interfaces\Result $result, $url, $label, $plural)
{
	global $MAIN_CFG;
	if ($result->num_rows) {
		echo '<div style="padding:10px;">
		<h3>'.$MAIN_CFG->global->top.' '.$label.'</h3>
		<ol>';
		while (list($id, $title, $counter) = $result->fetch_row()) {
			echo '<li><a href="'.URL::index($url.$id).'">'.$title.'</a> - ('.Dragonfly::getKernel()->L10N->plural($counter,$plural).')</li>';
		}
		echo '</ol></div>';
	}
}

require_once('header.php');
OpenTable();

/* Top 10 read stories */
$result = $db->query("SELECT sid, title, counter FROM {$db->TBL->stories} WHERE ptime<=".time()." AND counter>0 {$queryalang} ORDER BY counter DESC LIMIT {$MAIN_CFG->global->top}");
showTop($result, "News&file=article&sid=", _READSTORIES, '%d views');

/* Top 10 most voted stories */
$result = $db->query("SELECT sid, title, ratings FROM {$db->TBL->stories} WHERE ptime<=".time()." AND ratings>0 {$queryalang} ORDER BY ratings DESC LIMIT {$MAIN_CFG->global->top}");
showTop($result, "News&file=article&sid=", _MOSTVOTEDSTORIES, '%d votes');

/* Top 10 best rated stories */
$result = $db->query("SELECT sid, title, score/ratings FROM {$db->TBL->stories} WHERE ptime<=".time()." AND score>0 {$queryalang} ORDER BY 3 DESC LIMIT {$MAIN_CFG->global->top}");
showTop($result, "News&file=article&sid=", _BESTRATEDSTORIES, '%d points');

/* Top 10 commented stories */
if ($MAIN_CFG['global']['articlecomm']) {
	$result = $db->query("SELECT sid, title, comments FROM {$db->TBL->stories} WHERE ptime<=".time()." AND comments>0 {$queryalang} ORDER BY comments DESC LIMIT {$MAIN_CFG->global->top}");
	showTop($result, "News&file=article&sid=", _COMMENTEDSTORIES, '%d comments');
}

/* Top 10 categories */
$result = $db->query("SELECT catid, title, counter FROM {$db->TBL->stories_cat} WHERE counter>0 ORDER BY counter DESC LIMIT {$MAIN_CFG->global->top}");
showTop($result, "News&catid=", _ACTIVECAT, '%d hits');

/* Top 10 articles in special sections */
if (is_active('Sections')) {
	$result = $db->query("SELECT artid, title, counter FROM {$db->TBL->seccont} {$queryslang} ORDER BY counter DESC LIMIT {$MAIN_CFG->global->top}");
	showTop($result, "Sections&op=viewarticle&artid=", _READSECTION, '%d views');
}

/* Top 10 users submitters */
$result = $db->query("SELECT user_id, username, counter FROM {$db->TBL->users} WHERE counter > 0 ORDER BY counter DESC LIMIT {$MAIN_CFG->global->top}");
showTop($result, "Your_Account&profile==", _NEWSSUBMITTERS, '%d items');

/* Top 10 Polls */
if (is_active('Surveys')) {
	$result = $db->query("SELECT a.poll_id, a.poll_title, SUM(b.option_count) FROM {$db->TBL->poll_desc} a
	LEFT JOIN {$db->TBL->poll_data} b on a.poll_id = b.poll_id
	GROUP BY a.poll_id,a.poll_title,a.voters ORDER BY voters DESC LIMIT {$MAIN_CFG->global->top}");
	showTop($result, "Surveys&pollid=", _VOTEDPOLLS, '%d votes');
}

/* Top 10 reviews */
if (is_active('Reviews')) {
	$result = $db->query("SELECT id, title, hits FROM {$db->TBL->reviews} WHERE hits>0 $queryrlang ORDER BY hits DESC LIMIT {$MAIN_CFG->global->top}");
	showTop($result, "Reviews&op=showcontent&id=", _READREVIEWS, '%d views');
}

/* Top 10 Pages in Content */
if (is_active('Content')) {
	$result = $db->query("SELECT pid, title, counter FROM {$db->TBL->pages} WHERE active=1 AND counter>0 ORDER BY counter DESC LIMIT {$MAIN_CFG['global']['top']}");
	showTop($result, "Content&pa=showpage&pid=", _MOSTREADPAGES, '%d views');
}

CloseTable();
